import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import vuexPersist from "vuex-persist";
export default new Vuex.Store({
  state: {
    obj: []
  },
  mutations: {
    add(state, value) { // 存储数据

      if (value == '') {
        alert('输入为空')
        return false
      }

      let flag = state.obj.some(item => item.name == value)
      if (!flag) {
        state.obj.unshift({
          name: value,
          id: new Date().getTime(),
          check: true,
        })
      } else {
        alert('输入重复')
      }



    },
    change() {}, // 搭桥
    remove(state, id) { // 删除
      state.obj.splice(id, 1)
    },
    edit() {},
    removeAll(state) {
      state.obj = []
    }

  },
  actions: {},
  modules: {

  },
  getters: {
    num(state) {
      return state.obj.reduce((num, item) => num += item.check, 0)
    },
    total(state) {
      return state.obj.reduce((num, item) => num += !item.check, 0)
    }
  },
  plugins: [
    new vuexPersist({
      storage: window.localStorage,
    }).plugin,
  ],
})